package org.speed.mes.base.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.speed.mes.base.domain.vo.BsWorkshopVo;
import org.speed.mes.base.service.IBsWorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.speed.mes.base.domain.bo.BsWorklineBo;
import org.speed.mes.base.domain.vo.BsWorklineVo;
import org.speed.mes.base.domain.BsWorkline;
import org.speed.mes.base.mapper.BsWorklineMapper;
import org.speed.mes.base.service.IBsWorklineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产线管理Service业务层处理
 *
 * @author speed-mes
 * @date 2024-06-15
 */
@RequiredArgsConstructor
@Service
public class BsWorklineServiceImpl implements IBsWorklineService {

    private final BsWorklineMapper baseMapper;

    @Autowired
    private IBsWorkshopService workshopService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询产线管理
     */
    @Override
    public BsWorklineVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产线管理列表
     */
    @Override
    public TableDataInfo<BsWorklineVo> queryPageList(BsWorklineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsWorkline> lqw = buildQueryWrapper(bo);
        Page<BsWorklineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<BsWorklineVo> records = result.getRecords();
        records.stream().forEach(item -> {
            BsWorkshopVo workshop = workshopService.queryById(item.getWorkshopId());
            item.setWorkshopName(workshop.getName());
            SysUserVo user = userService.selectUserById(item.getUserId());
            item.setUserName(user.getUserName());
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询产线管理列表
     */
    @Override
    public List<BsWorklineVo> queryList(BsWorklineBo bo) {
        LambdaQueryWrapper<BsWorkline> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsWorkline> buildQueryWrapper(BsWorklineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsWorkline> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BsWorkline::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsWorkline::getName, bo.getName());
        lqw.eq(bo.getWorkshopId() != null, BsWorkline::getWorkshopId, bo.getWorkshopId());
        lqw.eq(bo.getUserId() != null, BsWorkline::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getShiftSystem()), BsWorkline::getShiftSystem, bo.getShiftSystem());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BsWorkline::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增产线管理
     */
    @Override
    public Boolean insertByBo(BsWorklineBo bo) {
        BsWorkline add = MapstructUtils.convert(bo, BsWorkline.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产线管理
     */
    @Override
    public Boolean updateByBo(BsWorklineBo bo) {
        BsWorkline update = MapstructUtils.convert(bo, BsWorkline.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsWorkline entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产线管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
